<?php

namespace App\Recharge\Logic;

use App\Models\NsSupplierGoods;
use App\Models\XnBatchOrder;
use App\Traits\Create;

use App\Recharge\Bean\BasicInformation;
use Illuminate\Database\Eloquent\Builder;
use App\Recharge\Bean\SupplierApiConfigBean;
use App\Models\InterfaceConfiguration;

use App\Recharge\Exceptions\SuspensionOfEnforcement;

class ConfigLogic extends BaseLogic
{
    /**
     * 供应商自定义接口配置检测
     *
     * @param InterfaceConfiguration $api 第三方接口
     * @return SupplierApiConfigBean
     * @throws SuspensionOfEnforcement
     */
    public function supplierApiConfigCheck(InterfaceConfiguration $api): SupplierApiConfigBean
    {
        $bean = new SupplierApiConfigBean();
        // 检测appid_title
        if ($api->appid_title) {
            $bean->setAppid($api->appid_title);
        }
        // 检测secret_key
        if ($api->private_key_title) {
            $bean->setSecretKey($api->private_key_title);
        }
        if ($api->iv_open == 2) {
            $bean->setIv($api->iv);
        }
        return $bean;
    }

    /**
     * @param InterfaceConfiguration $api 第三方接口
     * @param XnBatchOrder $order 订单
     * @param NsSupplierGoods|array $parent 主商品
     * @param SupplierApiConfigBean|null $config 供应商自定义配置
     * @param array $child 子商品
     * @return BasicInformation
     * @throws SuspensionOfEnforcement
     */
    public function makeBasicInformation(InterfaceConfiguration $api, XnBatchOrder $order, NsSupplierGoods|array $parent, ?SupplierApiConfigBean $config = null, array $child = []): BasicInformation
    {
        if (!is_array($parent)) {
            $parent = $parent->toArray();
        }
        if (empty($child)) {
            $child = $parent;
        }
        $basic = new BasicInformation();
        if (!$config) {
            $config = $this->supplierApiConfigCheck($api);
        }
        $basic->setSupplierNum($config->getAppid());
        $basic->setSecretKey($config->getSecretKey());
        $basic->setPrivateKey($config->getIv());
        // 回调地址
        $basic->setNotifyUrl(sprintf('%s/api/notify/%s', env('NOTIFY_URL'), $api->en));

        $basic->setOrderNo($order->orderNum);
        $basic->setGoodsId($child['goodsId']);
        $basic->setRechargeNum($order->rechargeNum);
        $basic->setStartCharging($parent['Start_charging']);
        $basic->setAccount($order->userName);
        $basic->setUnitPrice($child['unitPrice']);
        $basic->setIp($order->ip ?? '');
        $basic->setFaceValue($child['faceValue'] ?? 0);
        $basic->setAccountType($child['account_type'] ?? '');
        $basic->setGoodsName($order->goodsName);
        $basic->setGameAreaId('');
        $basic->setGameAreaName('');
        $basic->setGameServerId('');
        $basic->setGameServerName('');
        $basic->setProductId($child['productId'] ?? '');
        $basic->setProductType($child['productType'] ?? 0);
        $basic->setGameAcct('');
        $basic->setGameType($child['game_type'] ?? '');
        $basic->setDashangAccountType($child['dashang_account_type'] ?? 0);
        $basic->setCateId($child['cate_id'] ?? 0);
        $basic->setOperatorCode($child['operator_code'] ?? 0);
        $basic->setGoodsType($parent['goods_type'] ?? 0);
        return $basic;
    }

    /**
     * 获取第三方接口配置
     *
     * @param int $goods_belong 第三方归属id
     * @return InterfaceConfiguration
     * @throws SuspensionOfEnforcement
     */
    public function getApiConfig(int $goods_belong): InterfaceConfiguration
    {
        $api = InterfaceConfiguration::query()->firstWhere('id',$goods_belong);
        if (!$api) {
            throw new SuspensionOfEnforcement('未找到第三方配置信息');
        }
        return $api;
    }
}
